//package com.daxt.common.aspects;
//
//
//import com.alibaba.fastjson.JSON;
//import com.daxt.base.common.util.JwtUtil;
//import com.daxt.base.common.util.RedisUtil;
//import com.daxt.base.common.util.TokenInfo;
//import com.daxt.common.annotations.LoginAuth;
//import com.daxt.common.result.Result;
//import com.daxt.common.result.ResultCode;
//import com.daxt.common.result.ResultUtil;
//import com.daxt.model.service.base.result.UserInfo;
//import io.jsonwebtoken.Claims;
//import io.jsonwebtoken.Jws;
//import org.apache.commons.lang3.StringUtils;
//import org.apache.poi.ss.formula.functions.T;
//import org.aspectj.lang.ProceedingJoinPoint;
//import org.aspectj.lang.annotation.Around;
//import org.aspectj.lang.annotation.Aspect;
//import org.aspectj.lang.annotation.Before;
//import org.aspectj.lang.annotation.Pointcut;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//import org.springframework.web.context.request.RequestAttributes;
//import org.springframework.web.context.request.RequestContextHolder;
//import org.springframework.web.context.request.ServletRequestAttributes;
//
//import javax.servlet.ServletResponse;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
//import java.io.PrintWriter;
//import java.util.Map;
//
//@Aspect
//@Component
//public class LoginAspect {
//
//    @Autowired
//    private RedisUtil redisUtil;
//
//    @Pointcut(value = "@annotation(com.daxt.common.annotations.LoginAuth)")
//    public void access() {
//    }
//
//    @Before("access()")
//    public void before() {
//        System.out.println("开始验证用户是否登录...");
//    }
//
//    /**
//     * 定义切点
//     */
//    @Pointcut("@annotation(com.daxt.common.annotations.LoginAuth)")
//    public void privilege(){}
//
//
//    @Around("privilege()")
//    public Object around(ProceedingJoinPoint pj) {
//
//        try {
//
//            // 检验是否登录 true 已经登录  false 未登录
//            boolean flag = false;
//            RequestAttributes ra = RequestContextHolder.getRequestAttributes();
//            ServletRequestAttributes sra = (ServletRequestAttributes) ra;
//            HttpServletRequest request = sra.getRequest();
//            HttpServletResponse response = sra.getResponse();
//
//            String token = request.getHeader("Authorization");
//
//            if (StringUtils.isNotBlank(token)) {
//                Map<String, Object> checkJWT = JwtUtil.checkJWT(token);
//                if ((boolean) checkJWT.get("result")) {
//                    // Jws<Claims> parseJWT
//                    Map<String, Object> parseJWT1 = JwtUtil
//                            .parseJWT(JwtUtil.generateKey(JwtUtil.JWT_ALG, JwtUtil.JWT_RULE), token);
//                    if (!(boolean) parseJWT1.get("result")) {
//                        String code = parseJWT1.get("code").toString();
//                        ResultCode valueOf = ResultCode.valueOf(code);
//                        return ResultUtil.error(valueOf.getMes());
//                    }
//
//                    @SuppressWarnings("unchecked")
//                    Jws<Claims> parseJWT = (Jws<Claims>) parseJWT1.get("code");
//                    String userId = parseJWT.getBody().getId();
//                    String subject = parseJWT.getBody().getSubject();
//                    String key= TokenInfo.getKey(userId,subject);
//                    boolean hasKey = redisUtil.hasKey(key);
//                    if (hasKey) {
//						UserInfo userInfo = (UserInfo) redisUtil.get(key);
//						if(!userInfo.getToken().equals(token)) {
//                            return ResultUtil.error(ResultCode.TOKEN_SIGNATURE.getMes());
//						}
//                        return null;
////                        return ResultUtil.success();
//
//                    } else {
//                        return ResultUtil.error(ResultCode.TOKEN_EXPIREDJWT.getMes());
//                    }
//
//                } else if(ResultCode.TOKEN_MALFORMEDJWT.equals(checkJWT.get("code").toString())){
//                    return ResultUtil.error(ResultCode.TOKEN_MALFORMEDJWT.getMes());
//                }else {
//                    String code = checkJWT.get("code").toString();
//                    ResultCode valueOf = ResultCode.valueOf(code);
//                    return ResultUtil.error(valueOf.getMes());
//                }
//            } else {
//                return ResultUtil.error(ResultCode.TOKEN_NULL.getMes());
//            }
//
//
//        } catch (Throwable throwable) {
//            return null;
//        }
//    }
////    @Around("privilege()")
////    public Object around(ProceedingJoinPoint pj) {
////
////        try {
////
////            // 检验是否登录 true 已经登录  false 未登录
////            boolean flag = false;
////            RequestAttributes ra = RequestContextHolder.getRequestAttributes();
////            ServletRequestAttributes sra = (ServletRequestAttributes) ra;
////            HttpServletRequest request = sra.getRequest();
////            HttpServletResponse response = sra.getResponse();
////
////            String token = request.getHeader("Authorization");
////
////            if (StringUtils.isNotBlank(token)) {
////                Map<String, Object> checkJWT = JwtUtil.checkJWT(token);
////                if ((boolean) checkJWT.get("result")) {
////                    // Jws<Claims> parseJWT
////                    Map<String, Object> parseJWT1 = JwtUtil
////                            .parseJWT(JwtUtil.generateKey(JwtUtil.JWT_ALG, JwtUtil.JWT_RULE), token);
////                    if (!(boolean) parseJWT1.get("result")) {
////                        String code = parseJWT1.get("code").toString();
////                        ResultCode valueOf = ResultCode.valueOf(code);
////                        returnJson(response, ResultUtil.data(valueOf.getCode(), valueOf.getMes(),null));
////                        return false;
////                    }
////
////                    @SuppressWarnings("unchecked")
////                    Jws<Claims> parseJWT = (Jws<Claims>) parseJWT1.get("code");
////                    String userId = parseJWT.getBody().getId();
////                    String subject = parseJWT.getBody().getSubject();
////                    String key= TokenInfo.getKey(userId,subject);
////                    boolean hasKey = redisUtil.hasKey(key);
////                    if (hasKey) {
////						UserInfo userInfo = (UserInfo) redisUtil.get(key);
////						if(!userInfo.getToken().equals(token)) {
////							returnJson(response,ResultUtil.data(ResultCode.TOKEN_SIGNATURE.getCode(), ResultCode.TOKEN_SIGNATURE.getMes(), null));
////							return false;
////						}
////                        returnJson(response,ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), ResultCode.RESULT_SUCCESS.getMes(), null));
////                        return true;
////
////                    } else {
////                        returnJson(response,ResultUtil.data(ResultCode.TOKEN_EXPIREDJWT.getCode(), ResultCode.TOKEN_EXPIREDJWT.getMes(), null));
////                        return false;
////                    }
////
////                } else if(ResultCode.TOKEN_MALFORMEDJWT.equals(checkJWT.get("code").toString())){
////					/* TokenInfo tokenInfo = JwtUtil.parseExpriToken(token);
////					if (tokenInfo!=null) {
////						String key = TokenInfo.getKey(tokenInfo.getId(),tokenInfo.getFrom());
////						if(redisUtil.hasKey(key)) {
////							returnJson(response,ResultUtil.data(ResultCode.TOKEN_SIGNATURE.getCode(), ResultCode.TOKEN_SIGNATURE.getMes(), null));
////							return false;
////						}
////					}
////					return true;*/
////                    returnJson(response,ResultUtil.data(ResultCode.TOKEN_MALFORMEDJWT.getCode(), ResultCode.TOKEN_MALFORMEDJWT.getMes(), null));
////                    return false;
////                }else {
////                    String code = checkJWT.get("code").toString();
////                    ResultCode valueOf = ResultCode.valueOf(code);
////                    returnJson(response,ResultUtil.data(valueOf.getCode(), valueOf.getMes(), null));
////                    return false;
////                }
////            } else {
////                returnJson(response,ResultUtil.data(ResultCode.TOKEN_NULL.getCode(), ResultCode.TOKEN_NULL.getMes(), null));
////                return false;
////            }
////
////
////        } catch (Throwable throwable) {
////            return null;
////        }
////    }
//
//
//
//    private void returnJson(ServletResponse response, Result<T> result){
//        PrintWriter writer = null;
//        response.setCharacterEncoding("UTF-8");
//        response.setContentType("text/html; charset=utf-8");
//        try {
//            writer = response.getWriter();
//            writer.print(JSON.toJSONString(result));
//
//        } catch (IOException e) {
//            //log.error("response error",e);
//        } finally {
//            if (writer != null)
//                writer.close();
//        }
//    }
//}
